using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using BBX.Common;
using BBX.Config;
using BBX.Entity;
using BBX.Forum;
using NewLife;

namespace BBX.Web
{
    public class forumindex : PageBase
    {
        public List<IXForum> forumlist = new List<IXForum>();
        public List<Online> onlineuserlist = new List<Online>();
        //public List<PrivateMessageInfo> pmlist;
        public String lastvisit = "未知";
        public DataTable forumlinklist = ForumLink.FindAllWithCache().ToDataTable();
        public List<Announcement> announcementlist = new List<Announcement>();
        public String[] pagewordad = new String[0];
        public String doublead;
        public String floatad;
        public String mediaad;
        public String inforumad;
        public List<String> pagead = new List<String>();
        public Int32 announcementcount;
        public String onlineiconlist = "";
        public IUser userinfo;
        public Int32 totaltopic;
        public Int32 totalpost;
        public Int32 totalusers;
        public Int32 todayposts;
        public Int32 yesterdayposts;
        public Int32 highestposts;
        public String highestpostsdate = "";
        public Int32 forumlinkcount;
        public String lastusername = "";
        public Int32 lastuserid;
        public Int32 totalonline;
        public Int32 totalonlineuser;
        public Int32 totalonlineguest;
        public Int32 totalonlineinvisibleuser;
        public String highestonlineusercount = "";
        public String highestonlineusertime = "";
        public Boolean showforumonline;
        public Boolean isactivespace;
        public Boolean isallowapply;
        public String[] score = Scoresets.GetValidScoreName();
        public String navhomemenu = "";
        public Boolean showpmhint;
        public Tag[] taglist;
        public Int32 maxsubcount = GeneralConfigInfo.Current.Maxindexsubforumcount;
        public String templatelistboxoptionsforforumindex = Caches.GetTemplateListBoxOptionsCache(true);
        public ForumHotConfigInfo forumhotconfiginfo = ForumHotConfigInfo.Current;
        public Boolean disablepostctrl;

        protected override void ShowPage()
        {
            pagetitle = "首页";
            if (userid > 0 && useradminid > 0)
            {
                var adminGroupInfo = AdminGroup.FindByID(usergroupid);
                if (adminGroupInfo != null)
                {
                    disablepostctrl = adminGroupInfo.DisablePostctrl;
                }
            }
            var num = DNTRequest.GetInt("f", 1);
            if (num == 0)
            {
                ForumUtils.WriteCookie("isframe", "1");
            }
            else
            {
                num = ForumUtils.GetCookie("isframe").ToInt(-1);
                if (num == -1) num = config.Isframeshow;
            }
            if (num == 2)
            {
                HttpContext.Current.Response.Redirect(BaseConfigs.GetForumPath + "frame.aspx");
                HttpContext.Current.Response.End();
                return;
            }
            if (config.Rssstatus == 1)
            {
                base.AddLinkRss("tools/rss.aspx", "最新主题");
            }
            Online.UpdateAction(olid, UserAction.IndexShow, 0, config.Onlinetimeout);
            if (userid != -1)
            {
                userinfo = BBX.Entity.User.FindByID(userid);
                if (userinfo == null)
                {
                    userid = -1;
                    ForumUtils.ClearUserCookie();
                }
                else
                {
                    //this.newpmcount = !userinfo.Newpm ? 0 : this.newpmcount;
                    if (!userinfo.Newpm) newpmcount = 0;
                    lastvisit = userinfo.LastVisit.ToString();
                    showpmhint = (userinfo.NewsLetter.ToInt() > 4);
                }
            }
            navhomemenu = Caches.GetForumListMenuDivCache(usergroupid, userid, config.Extname);
            forumlist = Forums.GetForumIndexCollection(config.Hideprivate, usergroupid, config.Moddisplay, out totaltopic, out totalpost, out todayposts);
            forumlinkcount = forumlinklist.Rows.Count;
            //if (this.config.Enablespace == 1)
            //{
            //    this.GetSpacePerm();
            //}
            totalusers = Statistic.Current.TotalUsers;
            lastusername = Statistic.Current.LastUserName + "";
            lastuserid = Statistic.Current.LastUserID;
            yesterdayposts = Statistic.Current.YesterdayPosts;
            highestposts = Statistic.Current.HighestPosts;
            highestpostsdate = Statistic.Current.HighestPostsDate + "";
            if (todayposts > highestposts)
            {
                highestposts = todayposts;
                highestpostsdate = DateTime.Now.ToString("yyyy-M-d");
            }
            totalonline = onlineusercount;
            showforumonline = false;
            onlineiconlist = OnlineList.GetOnlineGroupIconList();
            if (totalonline < config.Maxonlinelist || DNTRequest.GetString("showonline") == "yes")
            {
                showforumonline = true;
                var list = Online.GetList(0, Online._.UserID, true);
                // 根据活跃时间降序
                list.Sort(Online._.LastActivity, true);
                onlineuserlist = list;

                var st = Online.GetStat();
                totalonline = st.Total;
                totalonlineuser = st.User;
                totalonlineinvisibleuser = st.Invisible;
                totalonlineguest = st.Guest;
            }
            if (DNTRequest.GetString("showonline") == "no")
            {
                showforumonline = false;
            }
            highestonlineusercount = Statistic.Current.HighestOnlineUserCount + "";
            highestonlineusertime = Statistic.Current.HighestOnlineUserTime.ToString("yyyy-MM-dd HH:mm");
            //this.announcementlist = Announcements.GetSimplifiedAnnouncementList(this.nowdatetime, "2999-01-01 00:00:00");
            announcementlist = Announcement.GetAvailableList();
            announcementcount = announcementlist != null ? announcementlist.Count : 0;
            var fs = new List<IXForum>();
            foreach (var current in forumlist)
            {
                current.Description = UBB.ParseSimpleUBB(current.Description);
                if (current.Layer == 0)
                {
                    fs.Add(current);
                }
            }
            taglist = config.Enabletag ? Tag.GetHotForumTags(config.Hottagcount).ToArray() : new Tag[0];
            headerad = Advertisement.GetOneHeaderAd("indexad", 0);
            footerad = Advertisement.GetOneFooterAd("indexad", 0);
            inforumad = Advertisement.GetInForumAd("indexad", 0, fs, templatepath);
            pagewordad = Advertisement.GetPageWordAd("indexad", 0);
            doublead = Advertisement.GetDoubleAd("indexad", 0);
            floatad = Advertisement.GetFloatAd("indexad", 0);
            mediaad = Advertisement.GetMediaAd(templatepath, "indexad", 0);
            pagead = Advertisement.GetPageAd("indexad", 0);
            if (userid > 0 && oluserinfo.Newpms < 0)
            {
                Users.UpdateUserNewPMCount(userid, olid);
            }
        }
    }
}